package com.github.why168;

import java.util.*;
import java.util.concurrent.DelayQueue;

public class Stack {
    public static void main(String[] args) {

        // 栈。先进后出 push、pop
        System.out.println("Stack:");
        java.util.Stack<Integer> jdkStack = new java.util.Stack<Integer>();
        jdkStack.push(1);
        jdkStack.push(2);
        jdkStack.push(3);
        jdkStack.push(4);
        jdkStack.push(5);
        int size = jdkStack.size();
        for (int i = 0; i < size; i++) {
            System.out.println(jdkStack.pop());
        }

        // 队列。先进先出 offer pop
        System.out.println("ArrayDeque:");
        ArrayDeque<Integer> deque = new ArrayDeque<Integer>();
        deque.offer(1);
        deque.offer(2);
        deque.offer(3);
        deque.offer(4);
        deque.offer(5);
        size = deque.size();
        for (int i = 0; i < size; i++) {
            System.out.println(deque.pop());
        }


    }

    public static void test() {
        int length = 5000;
        int max = length;
        // 生成一个长度为length，值从1~max的随机数组
        int[] data = GetRandomIntArray(1, max, length);
        int loopCount = 10;
        long t1, t2;

        t1 = System.currentTimeMillis();
        for (int i = 0; i < loopCount; i++) {
            testArrayDeque(data);
//            testLinkedList(data);
        }
        t2 = System.currentTimeMillis();

        // 测试loopCount次取平均结果
        System.out.println("timeTaken: " + String.format("%.1f", (t2 - t1) / (double) loopCount));
    }


    /**
     * 生成int类型随机的数组
     *
     * @param min   最小值
     * @param max   最大值
     * @param count 个数
     * @return
     */

    public static int[] GetRandomIntArray(int min, int max, int count) {
        //得到一个最大的数组(随机数的范围)
        int len = max - min + 1;
        if (max < min || count > len) {
            return null;
        }

        //生成一个空的结果随机数组
        int[] result = new int[count];
        //得到随机数数组
        int[] source = new int[len];
        //给随机数数组倒序赋值
        /* 对循环进行改进  */
//        for(int i=min,j=0;j<len;i++,j++) {
//            source[len -j -1 ] = i;
//        }
        for (int i = min; i <= max; i++) {
            source[i - min] = i;
        }

        //生成随机数
        Random r = new Random();
        int index = 0;
        int random;
        for (int i = 0; i < result.length; i++) {
            random = r.nextInt();
            index = Math.abs(random % len--);
            result[i] = source[index];
            source[index] = source[len];
        }
        return result;

    }

    public static void testArrayDeque(int[] data) {
        int length = data.length;
        Deque<Integer> stack = new ArrayDeque<>();
        for (int i = 0; i < length / 2; i++) {
            stack.offer(data[i]);
            stack.offer(data[i + 1]);
            stack.pop();
            stack.offer(stack.peek() + 1);
        }
    }

    public static void testLinkedList(int[] data) {
        int length = data.length;
        Deque<Integer> stack = new LinkedList<>();
        for (int i = 0; i < length / 2; i++) {
            stack.offer(data[i]);
            stack.offer(data[i + 1]);
            stack.pop();
            stack.offer(stack.peek() + 1);
        }
    }
}


